package com.itheima.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.ClazzMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.Emp;
import com.itheima.pojo.PageResult;
import com.itheima.service.ClazzService;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ClazzServiceImpl extends ServiceImpl<ClazzMapper, Clazz> implements ClazzService {
    @Autowired
    private EmpService empService;
    @Autowired
    private ClazzMapper clazzMapper;

    /**
     * 条件分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @param begin
     * @param end
     * @return
     */
    @Override
    public PageResult queryPage(int page, int pageSize, String name, LocalDate begin, LocalDate end) {
        //1.设置分页、排序参数
        Page<Clazz> objectPagepage = Page.of(page, pageSize);
        objectPagepage.addOrder(OrderItem.desc("update_time"));
        //2.构造查询条件
        //3.发起分页查询
        //2+3(主流)

        Page<Clazz> clazzPage = this.lambdaQuery().like(StrUtil.isNotBlank(name), Clazz::getName, name)
                .between(ObjectUtil.isNotNull(begin) && ObjectUtil.isNotNull(end), Clazz::getBeginDate, begin, end)
                .page(objectPagepage);
        //4.对结果集进行处理
        List<Clazz> records = clazzPage.getRecords();
        records.forEach(clazz -> {
            Emp emp = empService.getById(clazz.getMasterId());
            //设置状态
            if (ObjectUtil.isNotNull(clazz.getBeginDate()) && LocalDate.now().isBefore(clazz.getBeginDate())) {
                clazz.setStatus("未开课");
            } else if (ObjectUtil.isNotNull(clazz.getEndDate()) && LocalDate.now().isAfter(clazz.getEndDate())) {
                clazz.setStatus("已结束");
            } else {
                clazz.setStatus("在读");
            }
            //设置班主任(在员工中获取数据)
            if (ObjectUtil.isNotNull(emp)) {
                clazz.setMasterName(emp.getName());
            }
        });
        //5.返回分页结果集
        return new PageResult<>(clazzPage.getTotal(), records);
    }

    /**
     * 新增班级
     *
     * @param clazz
     */
    @Override
    public void insertClazz(Clazz clazz) {
        //设置创建时间
        clazz.setCreateTime(LocalDateTime.now());

        //设置修改时间
        clazz.setUpdateTime(LocalDateTime.now());

        //新增班级
        this.clazzMapper.insert(clazz);

        //设置班主任
        Emp emp = empService.getById(clazz.getMasterId());
        if (ObjectUtil.isNotNull(emp)) {
            clazz.setMasterName(emp.getName());
        }
    }

    /**
     * 根据id查询班级
     *
     * @param id
     * @return
     */
    @Override
    public Clazz getClazzById(Integer id) {
        //查询班级
        Clazz clazz = this.clazzMapper.selectById(id);
        //查询班主任
        Emp emp = empService.getById(clazz.getMasterId());
        if (ObjectUtil.isNotNull(emp)) {
            //设置班主任
            clazz.setMasterName(emp.getName());
        }
        return clazz;
    }

    /**
     * 修改班级
     *
     * @param clazz
     */
    @Override
    public void updateClazz(Clazz clazz) {
        //1.设置修改时间
        clazz.setUpdateTime(LocalDateTime.now());
        //2.修改班级
        this.clazzMapper.updateById(clazz);
        //3.修改班主任
        Emp emp = empService.getById(clazz.getMasterId());
        if (ObjectUtil.isNotNull(emp)) {
            clazz.setMasterName(emp.getName());
        }
    }

    /**
     * 删除班级
     *
     * @param id
     */
    @Override
    public void deleteClazz(Integer id) {
        //1.删除班级
        this.clazzMapper.deleteById(id);
    }
}
